var player = 
{
	playerId:"",
	speed:12,
	attack:1,
	hp:100,
	canvasNum:0,
	
	birth_x:266,
	birth_y:179,
	curr_hp:80,
	
	screen_x:769,
	screen_y:515,
	x:769,
	y:515,
	length_x:50,
	length_y:100,
	K_UP:38,
	K_XX:32,
	K_ZZ:90,
	K_DOWN:40,
	K_LEFT:37,
	K_RIGHT:39,
	position:1,
	score:0,
	angle:0,
	playerBomb:"player_bomb",
	alive:1,
	goCircle:1,
	moved:0,
	delay:1,
	lastBullet:-1,
	lastSuper:-1,
	beHurtId:"",
	beHurtNum:-1,
	onFire:1,//当前fire音效
	superState:0,
	kind:-1,
	h:0,


	draw:function()
	{
		
		var img_playerBomb = document.getElementById(this.playerBomb);//get the bomb image object element

		if(this.goCircle==1){
			if(this.alive==1){
				g.save();
				
				g.translate(this.birth_x,this.birth_y);
				if((  Math.abs( ((1/(Math.PI*2)) * this.angle) %(Math.PI*2) )<=(Math.PI/2))||(Math.abs( ((1/(Math.PI*2)) * this.angle) %(Math.PI*2) )>=(Math.PI*1.5))){
					g.fillStyle="#7A378B";
					g.fillRect(-this.length_x/2+8, -this.length_y/2-2,this.hp/5+4,9);
					g.fillStyle="black";
					g.fillRect(-this.length_x/2+10, -this.length_y/2,this.hp/5,5);
					g.fillStyle="red";
					g.fillRect(-this.length_x/2+10, -this.length_y/2,this.curr_hp/5,5);
					g.fillStyle="black";
				}
				else {
					g.fillStyle="#7A378B";
					g.fillRect(-this.length_x/2+8, -this.length_y/2+this.speed-2,this.hp/5+4,9);
					g.fillStyle="black";
					g.fillRect(-this.length_x/2+10, -this.length_y/2+this.speed,this.hp/5,5);
					g.fillStyle="red";
					g.fillRect(-this.length_x/2+10, -this.length_y/2+this.speed,this.curr_hp/5,5);
					g.fillStyle="black";
				}
				
				g.rotate( (1/(Math.PI*2)) * this.angle );
				//绘制阴影
				//g.drawImage(img_playerShadow, this.birth_x-10, this.birth_y+20, this.length_x, this.length_y) ;
				
				//绘制飞机
				//alert(canvas[0]);
				
				if(this.superState==1){
					//alert(this.superState+"player superState");
					if(this.kind==1){
						g.drawImage(speedUp, -this.length_x/2+5, -this.length_y/2-10+60 , 40, 171) ;
					}
					else if(this.kind==2){
						g.drawImage(noHurt, -this.length_x/2-30, -this.length_y/2-10 , 110, 110) ;
					}
					
				}
				g.drawImage(canvas[this.canvasNum], -this.length_x/2, -this.length_y/2 , this.length_x, this.length_y) ;
				g.restore();
			}

			
		}
		else if(this.goCircle==0){
			

			if(this.alive==1){
				g.save();
				g.translate(this.birth_x,this.birth_y);
				
				
				
				//绘制阴影
				//g.drawImage(img_playerShadow, this.birth_x-10, this.birth_y+20, this.length_x, this.length_y) ;
				
				
				//绘制飞机
				if(this.position==1){	
					g.fillStyle="#7A378B";
					g.fillRect(-this.length_x/2+8, -this.length_y/2+this.speed-2,this.hp/5+4,9);
					g.fillStyle="black";
					g.fillRect(-this.length_x/2+10, -this.length_y/2+this.speed,this.hp/5,5);
					g.fillStyle="red";
					g.fillRect(-this.length_x/2+10, -this.length_y/2+this.speed,this.curr_hp/5,5);
					g.fillStyle="black";
					
					g.rotate( (1/(Math.PI*2)) * this.angle );
					if(this.superState==1){
						if(this.kind==1){
							g.drawImage(speedUp, -this.length_x/2+5, -this.length_y/2-10+60+this.speed, 40, 171) ;
						}
						else if(this.kind==2){
							g.drawImage(noHurt, -this.length_x/2-30, -this.length_y/2-10+this.speed , 110, 110) ;
						}
						
					}
					g.drawImage(canvas[this.canvasNum], -this.length_x/2, (-this.length_y/2)+this.speed , this.length_x, this.length_y);
				}
				else {
					if(  Math.abs( ((1/(Math.PI*2)) * this.angle) %(Math.PI*2) )<=(Math.PI/2)){
						g.fillStyle="#7A378B";
						g.fillRect(-this.length_x/2+8, -this.length_y/2-this.speed-2,this.hp/5+4,9);
						g.fillStyle="black";
						g.fillRect(-this.length_x/2+10, -this.length_y/2-this.speed,this.hp/5,5);
						g.fillStyle="red";
						g.fillRect(-this.length_x/2+10, -this.length_y/2-this.speed,this.curr_hp/5,5);
						g.fillStyle="black";
					}
					else {
						g.fillStyle="#7A378B";
						g.fillRect(-this.length_x/2+8, -this.length_y/2+this.speed*2-2,this.hp/5+4,9);
						g.fillStyle="black";
						g.fillRect(-this.length_x/2+10, -this.length_y/2+this.speed*2,this.hp/5,5);
						g.fillStyle="red";
						g.fillRect(-this.length_x/2+10, -this.length_y/2+this.speed*2,this.curr_hp/5,5);
						g.fillStyle="black";
					}
					
					g.rotate( (1/(Math.PI*2)) * this.angle );
					if(this.superState==1){
						if(this.kind==1){
							g.drawImage(speedUp, -this.length_x/2+5, -this.length_y/2-10+60-this.speed, 40, 171) ;
						}
						else if(this.kind==2){
							g.drawImage(noHurt, -this.length_x/2-30, -this.length_y/2-10-this.speed , 110, 110) ;
						}
						
					}
					g.drawImage(canvas[this.canvasNum], -this.length_x/2, -this.length_y/2-this.speed, this.length_x, this.length_y) ;
					
				}
				if(this.moved==1){
					if(this.position==1){
						this.birth_x=this.birth_x+this.speed*(Math.sin((1/(Math.PI*2)) * this.angle));
						this.birth_y=this.birth_y-this.speed*(Math.cos((1/(Math.PI*2)) * this.angle));
					}
					else{
						this.birth_x=this.birth_x-this.speed*(Math.sin((1/(Math.PI*2)) * this.angle));
						this.birth_y=this.birth_y+this.speed*(Math.cos((1/(Math.PI*2)) * this.angle));
					}
					this.moved=0;
				}
				g.restore();
			}
			
			
		}
		else if(this.goCircle==3){
			if(this.alive==1){
				g.save();
			
				g.translate(this.birth_x,this.birth_y);
				
				

				//绘制阴影
				//g.drawImage(img_playerShadow, this.birth_x-10, this.birth_y+20, this.length_x, this.length_y) ;
				
				
				
				//绘制飞机
				if(this.position==1){
					g.fillStyle="#7A378B";
					g.fillRect(-this.length_x/2+8, -this.length_y/2+this.speed-2,this.hp/5+4,9);
					g.fillStyle="black";
					g.fillRect(-this.length_x/2+10, -this.length_y/2+this.speed,this.hp/5,5);
					g.fillStyle="red";
					g.fillRect(-this.length_x/2+10, -this.length_y/2+this.speed,this.curr_hp/5,5);
					g.fillStyle="black";
					
					g.rotate( (1/(Math.PI*2)) * this.angle );
					if(this.superState==1){
						if(this.kind==1){
							g.drawImage(speedUp, -this.length_x/2+5, -this.length_y/2-10+60+this.speed, 40, 171) ;
						}
						else if(this.kind==2){
							g.drawImage(noHurt, -this.length_x/2-30, -this.length_y/2-10+this.speed , 110, 110) ;
						}
						
					}
					g.drawImage(canvas[this.canvasNum], -this.length_x/2, -this.length_y/2+this.speed , this.length_x, this.length_y);
					
				}
				else {
					if(  Math.abs( ((1/(Math.PI*2)) * this.angle) %(Math.PI*2) )<=(Math.PI/2)){
						g.fillStyle="#7A378B";
						g.fillRect(-this.length_x/2+8, -this.length_y/2-this.speed-2,this.hp/5+4,9);
						g.fillStyle="black";
						g.fillRect(-this.length_x/2+10, -this.length_y/2-this.speed,this.hp/5,5);
						g.fillStyle="red";
						g.fillRect(-this.length_x/2+10, -this.length_y/2-this.speed,this.curr_hp/5,5);
						g.fillStyle="black";
					}
					else {
						g.fillStyle="#7A378B";
						g.fillRect(-this.length_x/2+8, -this.length_y/2+this.speed*2-2,this.hp/5+4,9);
						g.fillStyle="black";
						g.fillRect(-this.length_x/2+10, -this.length_y/2+this.speed*2,this.hp/5,5);
						g.fillStyle="red";
						g.fillRect(-this.length_x/2+10, -this.length_y/2+this.speed*2,this.curr_hp/5,5);
						g.fillStyle="black";
					}
					
					g.rotate( (1/(Math.PI*2)) * this.angle );
					if(this.superState==1){
						if(this.kind==1){
							g.drawImage(speedUp, -this.length_x/2+5, -this.length_y/2-10+60-this.speed, 40, 171) ;
						}
						else if(this.kind==2){
							g.drawImage(noHurt, -this.length_x/2-30, -this.length_y/2-10-this.speed , 110, 110) ;
						}
						
					}
					g.drawImage(canvas[this.canvasNum], -this.length_x/2, -this.length_y/2-this.speed, this.length_x, this.length_y) ;
					
				}
				if(this.moved==1){
					if(this.position==1){
						this.birth_x=this.birth_x+this.speed*(Math.sin((1/(Math.PI*2)) * this.angle));
						this.birth_y=this.birth_y-this.speed*(Math.cos((1/(Math.PI*2)) * this.angle));
					}
					else{
						this.birth_x=this.birth_x-this.speed*(Math.sin((1/(Math.PI*2)) * this.angle));
						this.birth_y=this.birth_y+this.speed*(Math.cos((1/(Math.PI*2)) * this.angle));
					}
					this.moved=0;
				}
				g.restore();
			}

		}
		else {
			if(this.alive==1){
				
				if(this.birth_x<20){
					if(this.superState==1){
						if(this.kind==1){
							g.drawImage(speedUp, -this.length_x/2+5, -this.length_y/2-10+60, 40, 171) ;
						}
						else if(this.kind==2){
							g.drawImage(noHurt, -this.length_x/2-30, -this.length_y/2-10 , 110, 110) ;
						}
						
					}
					g.drawImage(canvas[this.canvasNum], 21, this.birth_y , this.length_x, this.length_y) ;
					this.birth_x=21;
				}
				else if (((this.birth_x) > (this.screen_x-20)))
				{
					if(this.superState==1){
						if(this.kind==1){
							g.drawImage(speedUp, -this.length_x/2+5, -this.length_y/2-10+60, 40, 171) ;
						}
						else if(this.kind==2){
							g.drawImage(noHurt, -this.length_x/2-30, -this.length_y/2-10 , 110, 110) ;
						}
						
					}
					g.drawImage(canvas[this.canvasNum], (this.screen_x-21), this.birth_y , this.length_x, this.length_y) ;
					this.birth_x=(this.screen_x-21);
				}
				else if ((this.birth_y < (-this.length_x/2)))
				{
					if(this.superState==1){
						if(this.kind==1){
							g.drawImage(speedUp, -this.length_x/2+5, -this.length_y/2-10+60, 40, 171) ;
						}
						else if(this.kind==2){
							g.drawImage(noHurt, -this.length_x/2-30, -this.length_y/2-10 , 110, 110) ;
						}
						
					}
					g.drawImage(canvas[this.canvasNum], this.birth_x, (-this.length_x/2)+1 , this.length_x, this.length_y) ;
					this.birth_y=(-this.length_x/2)+1;
				}
				else if ((this.birth_y > (this.screen_y)))
				{
					if(this.superState==1){
						if(this.kind==1){
							g.drawImage(speedUp, -this.length_x/2+5, -this.length_y/2-10+60, 40, 171) ;
						}
						else if(this.kind==2){
							g.drawImage(noHurt, -this.length_x/2-30, -this.length_y/2-10 , 110, 110) ;
						}
						
					}
					g.drawImage(canvas[this.canvasNum], this.birth_x, (this.screen_y)-1 , this.length_x, this.length_y) ;
					this.birth_y=(this.screen_y)-1;
				}
				else
				{
					if(this.superState==1){
						if(this.kind==1){
							g.drawImage(speedUp, -this.length_x/2+5, -this.length_y/2-10+60, 40, 171) ;
						}
						else if(this.kind==2){
							g.drawImage(noHurt, -this.length_x/2-30, -this.length_y/2-10 , 110, 110) ;
						}
						
					}
					g.drawImage(canvas[this.canvasNum], this.birth_x, this.birth_y , this.length_x, this.length_y) ;
				}
				
				
			}
		}
			
	},
	
	addBullet1:function()
	{
		
		if((this.kind==3)&&(this.superState==1)){
			if(this.onFire==1){
				m_fire1.play();
				this.onFire++;
			}
			else if(this.onFire==2){
				m_fire2.play();
				this.onFire++;
			}
			else if(this.onFire==3){
				m_fire3.play();
				this.onFire++;
			}
			else if(this.onFire==4){
				m_fire1.play();
				this.onFire=1;
			}
			var bullet = new Bullet1(this.birth_x-15+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle,this.attack*1.2,this.playerId,(bullets.length));
			bullets.push(bullet);
			var bullet2 = new Bullet1(this.birth_x+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle,this.attack*1.2,this.playerId,(bullets.length));
			bullets.push(bullet2);
			var bullet3 = new Bullet1(this.birth_x+15+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle,this.attack*1.2,this.playerId,(bullets.length));
			bullets.push(bullet3);
		}
		else {
			if(this.onFire==1){
				m_fire1.play();
				this.onFire++;
			}
			else if(this.onFire==2){
				m_fire2.play();
				this.onFire++;
			}
			else if(this.onFire==3){
				m_fire3.play();
				this.onFire++;
			}
			else if(this.onFire==4){
				m_fire1.play();
				this.onFire=1;
			}
			var bullet0 = new Bullet1(this.birth_x+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle,this.attack,this.playerId,(bullets.length));
			bullets.push(bullet0);
		}
	},
	
	superZZ:function()
	{	
		if(this.kind==1){
			this.speed=(this.speed)*3;
			this.superState=1;
			var id_xx=this.playerId;
			setTimeout("sleepZZ();",3000);
		}
		else if(this.kind==2){
			this.superState=1;
			var id_xx=this.playerId;
			setTimeout("sleepZZ();",3000);
		}
		else if(this.kind==3){
			this.superState=1;
			m_fire4.play();
			this.addBullet1();
			var id_xx=this.playerId;
			setTimeout("sleepZZ();",3000);
		}
		else if(this.kind==0){
			var bullet = new Bullet1(this.birth_x+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle,(this.attack*2+70),this.playerId,(bullets.length));
			bullets.push(bullet);
			var bullet2 = new Bullet1(this.birth_x+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle+45,(this.attack*2+70),this.playerId,(bullets.length));
			bullets.push(bullet2);
			var bullet3 = new Bullet1(this.birth_x+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle+90,(this.attack*2+70),this.playerId,(bullets.length));
			bullets.push(bullet3);
			var bullet4 = new Bullet1(this.birth_x+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle+135,(this.attack*2+70),this.playerId,(bullets.length));
			bullets.push(bullet4);
			var bullet5 = new Bullet1(this.birth_x+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle+180,(this.attack*2+70),this.playerId,(bullets.length));
			bullets.push(bullet5);
			var bullet6 = new Bullet1(this.birth_x+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle-45,(this.attack*2+70),this.playerId,(bullets.length));
			bullets.push(bullet6);
			var bullet7 = new Bullet1(this.birth_x+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle-90,(this.attack*2+70),this.playerId,(bullets.length));
			bullets.push(bullet7);
			var bullet8 = new Bullet1(this.birth_x+(Math.sin((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.birth_y-(Math.cos((1/(Math.PI*2)) * this.angle))*(this.length_y/2),this.angle-135,(this.attack*2+70),this.playerId,(bullets.length));
			bullets.push(bullet8);
			this.curr_hp=this.curr_hp-400;
			this.h=1;
			this.superState=1;
			var id_xx=this.playerId;
			setTimeout("sleepZZ();",3000);
		}
	},
		
	keymove:function()
	{
		var v = this.speed;
		if(key[this.K_UP]){
			
			if((this.birth_x < 20)||((this.birth_x) > (this.screen_x-20))||(this.birth_y < (-this.length_x/2))||(this.birth_y > (this.screen_y))) {
				this.goCircle=4;
			}
			else{
				this.position=1;
				this.moved=1;
				this.goCircle=0;
				
				if(key[this.K_LEFT]){
					this.position=1;
					this.moved=1;
					this.angle=this.angle-0.2;
					this.goCircle=3;
					
				}
				else if(key[this.K_RIGHT]){
					this.position=1;
					this.moved=1;
					this.angle=this.angle+0.2;
					this.goCircle=3;
					
				}
			}
			setP(this.birth_x,this.birth_y,this.angle);
			key_function();
		}
		else if(key[this.K_DOWN]){
			if((this.birth_x < 20)||((this.birth_x) > (this.screen_x-20))||(this.birth_y < (-this.length_x/2))||(this.birth_y > (this.screen_y))) {
				this.goCircle=4;
			}
			else {
				this.position=0;
				this.moved=1;
				this.goCircle=0;
				
				if(key[this.K_LEFT]){
					this.position=0;
					this.moved=1;
					this.angle=this.angle-0.2;
					this.goCircle=3;
				}
				else if(key[this.K_RIGHT]){
					this.position=0;
					this.moved=1;
					this.angle=this.angle+0.2;
					this.goCircle=3;
				}
			}
			setP(this.birth_x,this.birth_y,this.angle);
			key_function();
		}
		else if(key[this.K_LEFT]){
			this.angle=this.angle-0.2;
			this.goCircle=1;
			setP(this.birth_x,this.birth_y,this.angle);
			key_function();
		}
		else if(key[this.K_RIGHT]){
			this.angle=this.angle+0.2;
			this.goCircle=1;
			setP(this.birth_x,this.birth_y,this.angle);
			key_function();
		}
		

		if(key[this.K_XX]){
			if((this.lastBullet==-1)||((time-this.lastBullet)>(this.delay*34))){
				this.addBullet1();
				this.lastBullet=time;
				xx_key();
				key_function();
			}
		}
		else if(key[this.K_ZZ]){
			if((this.lastSuper==-1)||((time-this.lastSuper)>(this.delay*1000))){
				this.superZZ();
				this.lastSuper=time;
				zz_key();
				key_function();
			}
			
		}
		else {
			
		}
		
	},
	
	checkHit : function()
	{
		//check if hurt
	
		for(var i = 0; i < enemy_bullets.length;i ++)
		{
			var ex = enemy_bullets[i].birth_x; 
			var ey = enemy_bullets[i].birth_y;
			var attack=enemy_bullets[i].attack;
			var center_x=this.birth_x;
			var center_y=this.birth_y;
			
			if((gameStart==1)&&(this.alive==1)&&( Math.sqrt( Math.abs(center_x-ex)*Math.abs(center_x-ex)+Math.abs(center_y-ey)*Math.abs(center_y-ey) )<(this.length_x/2) ))
			{	
				
				if((this.kind==2)&&(this.superState==1)){
					
				}
				else{
					this.curr_hp-=attack;
					enemy_bullets[i].isDie=true;
					this.beHurtId=enemy_bullets[i].playerId;
					this.beHurtNum=enemy_bullets[i].num_i;
					this.h=1;
					m_fireHit.play();
					zhen();
				}
			}
			
		}
		
		//END

	},

	update:function()
	{
		this.keymove();
		this.checkHit();
		if(this.h==1){
			
			if (this.curr_hp<=0){
				hurt(this.curr_hp);
				this.alive=0;
				quit();
				this.curr_hp=0;
			}
			else{
				hurt(this.curr_hp);
			}
		}
		this.h=0;
		
	}
	
}


function Bullet1(x,y,a,attack,playerId,num_i)
{
	this.birth_x = x;//出生位置 x
	this.birth_y = y;//出生位置 y
	this.speed = 10;
	this.angle=a;
	this.isDie = false;
	this.move_x=x+2*(Math.sin((1/(Math.PI*2)) * this.angle));
	this.move_y=y-2*(Math.cos((1/(Math.PI*2)) * this.angle));
	this.attack=attack;
	this.playerId=playerId;
	this.num_i=num_i;
}

Bullet1.prototype.update = function()
{
	this.move_x=this.move_x+this.speed*(Math.sin((1/(Math.PI*2)) * this.angle));
	this.move_y=this.move_y-this.speed*(Math.cos((1/(Math.PI*2)) * this.angle));

	this.birth_x=this.birth_x+(this.speed)*(Math.sin((1/(Math.PI*2)) * this.angle));
	this.birth_y=this.birth_y-(this.speed)*(Math.cos((1/(Math.PI*2)) * this.angle));

	if ((this.move_x>player.screen_x)||(this.move_x<0)||(this.move_y>player.screen_y)||(this.move_y<0))
	{
		this.isDie=true;

	}
};

Bullet1.prototype.draw = function()
{
	if (!this.isDie)
	{
	
		g.beginPath();
		
		
		g.strokeStyle = "blue";
		g.lineWidth="10";
		g.lineCap = "round";
		g.moveTo(this.birth_x,this.birth_y);
		g.lineTo(this.move_x,this.move_y);
		g.stroke();  
		
		/*
		g.moveTo(this.birth_x,this.birth_y);    
     
		var controlX = this.move_x-100;    
		var controlY = this.move_y-100;      
		 
		g.quadraticCurveTo(controlX, controlY, this.birth_x+3,this.birth_y+3);    
		g.lineWidth = 3;    
		g.strokeStyle = "blue"; // line color    
		g.stroke(); 
		*/
	}
};






